﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Nova_DAL;
using System.Data.Common;
using Nova_Common;
using System.Text;

public partial class DelStorage_DelDataPOP : System.Web.UI.Page
{
    DbCommand cmd = null;
    Nova_DAL.DbHelper access = new DbHelper();
    string strSQL = "";
    DataSet ds = null;
    string strStroageNo = "";
    int i = 0;
    string MType = "";
    string strPID = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            MType = Request["MType"].ToString();
            if (MType == "Edit")
            {
                try
                {
                    strPID = Request["PID"].ToString();
                }
                catch (Exception ex)
                {
                }
            }
        }
        catch
        {
        }
        panel();

        if (!IsPostBack)
        {
            try
            {
                strStroageNo = Request["StorageNo"].ToString();
                txt_StorageNo.Text = strStroageNo.ToString();
            }
            catch
            {
            }
            if (MType == "Edit")
            {
                bind();
            }



        }
    }


    protected void bind()
    {
        //strSQL = "SELECT TAS.Num,TAS.PID,TBM.Mat_Name,TBM.S01,TBM.Mat_Code,TAS.StockNo FROM TDelStorage_List TAS LEFT JOIN TBase_Material TBM ON TAS.Mat_Code = TBM.Mat_Code WHERE TAS.PID='" + strPID + "'";

        strSQL = @"SELECT TAS.Num,TAS.PID,TBM.Mat_Name,TBM.S01,TBM.TM01,TBM.TM03,TBM.S02,TAS.TS01,TAS.S05,TBM.Unit_Base,TBM.Mat_Code,TAS.StockNo
                FROM TDelStorage_List TAS
                LEFT JOIN TBase_Material TBM ON TAS.Mat_Code = TBM.Mat_Code
                WHERE TAS.PID = '" + strPID + "'";
        cmd = access.GetSqlStringCommond(strSQL);
        ds = access.ExecuteDataSet(cmd);
        if (ds.Tables[0].Rows.Count > 0)
        {
            txt_StorageNo.Text = ds.Tables[0].Rows[0]["StockNo"].ToString();
            txt_S01.Text = ds.Tables[0].Rows[0]["S01"].ToString();
            txtNum.Text = ds.Tables[0].Rows[0]["Num"].ToString();
            txt_Mat_Name.Text = ds.Tables[0].Rows[0]["Mat_Name"].ToString();
            txt_Mat_Code.Text = ds.Tables[0].Rows[0]["Mat_Code"].ToString();
            txt_SerialNumber.Text = ds.Tables[0].Rows[0]["TS01"].ToString();
            txt_TM01.Text = ds.Tables[0].Rows[0]["TM01"].ToString();
            txt_TM03.Text = ds.Tables[0].Rows[0]["TM03"].ToString();
            txt_S02.Text = ds.Tables[0].Rows[0]["S02"].ToString();
        }

    }

    protected void panel()
    {
        if (MType == "ADD")
        {
            add.Visible = true;
            edit.Visible = false;
        }
        else
        {
            add.Visible = false;
            edit.Visible = true;
        }
    }



    protected void ibtnOK_Click(object sender, ImageClickEventArgs e)
    {
        if (!CheckData())
        {
            return;
        }

        Random rad = new Random();
        string strW01 = txt_StorageNo.Text.Trim() + rad.Next(1000, 9999);
        strSQL = @"Insert Into TDelStorage_List(StockNo,Mat_Code,Num,W01,TS02,TS03,TS01)
        values('" + txt_StorageNo.Text + "','" + txt_Mat_Code.Text + "','" + txtNum.Text + "','" + strW01 + "','" + Request.Cookies["WCode"].Value + "','" + HttpUtility.UrlDecode(Request.Cookies["WName"].Value) + "','" + txt_SerialNumber.Text.Trim() + "')";
        cmd = access.GetSqlStringCommond(strSQL);
        i = access.ExecuteNonQuery(cmd);
        if (i > 0)
        {
            ShowBox.MessageBox(this.Page, "添加成功！");
            ibtnClose_Click(sender, e);
        }
        else
        {
            ShowBox.MessageBox(this.Page, "添加失败！");
        }

    }

    private bool CheckData()
    {
        if (txt_StorageNo.Text == "")
        {
            ShowBox.MessageBox(this.Page, "订单号不能为空！");
            return false;
        }
        else
        {
            strSQL = @"select count(1) from TBill_DelStorage where StockNo='" + txt_StorageNo.Text + "'";
            cmd = access.GetSqlStringCommond(strSQL);
            if (Convert.ToInt32(access.ExecuteScalar(cmd)) == 0)
            {
                ShowBox.MessageBox(this.Page, "请先保存订单号！");
                return false;

            }

        }

        if (txt_Mat_Code.Text == "")
        {
            ShowBox.MessageBox(this.Page, "物料编码不能为空！");
            return false;
        }


        if (txtNum.Text.ToString() == "")
        {
            ShowBox.MessageBox(this.Page, "数量不能为空！");
            return false;
        }

        strSQL = @"select count(1) from TDelStorage_List where StockNo='" + txt_StorageNo.Text + "' and Mat_Code='" + txt_Mat_Code.Text + "' and TS01 ='" + txt_SerialNumber.Text.Trim() + "'";
        cmd = access.GetSqlStringCommond(strSQL);
        if (Convert.ToInt32(access.ExecuteScalar(cmd)) > 0)
        {
            ShowBox.MessageBox(this.Page, "该物料编码已经存在该出库单中，可直接修改数量！");
            return false;
        }

        return true;

    }

    protected void ibtnContinue_Click(object sender, ImageClickEventArgs e)
    {


        if (!CheckData())
        {
            return;
        }

        Random rad = new Random();
        string strW01 = txt_StorageNo.Text.Trim() + rad.Next(1000, 9999);

        strSQL = "Insert Into TDelStorage_List(StockNo,Mat_Code,Num,W01,TS02,TS03) values('" + txt_StorageNo.Text + "','" + txt_Mat_Code.Text + "','" + txtNum.Text + "','" + strW01 + "','" + Request.Cookies["WCode"].Value + "','" + HttpUtility.UrlDecode(Request.Cookies["WName"].Value) + "')";
        cmd = access.GetSqlStringCommond(strSQL);
        access.ExecuteNonQuery(cmd);
        txt_Mat_Code.Text = "";
        txt_Mat_Name.Text = "";
        txtNum.Text = "";
       // txt_S01.Text = "";

    }
    protected void ibtnClose_Click(object sender, ImageClickEventArgs e)
    {
        //ShowBox.MessageBox(this.Page, "添加修改成功!");
        //this.Page.ClientScript.RegisterStartupScript(typeof(string), "Nova", "window.returnValue='" + txt_StorageNo.Text.ToString().Trim() + "';top.close();", true);
        this.Page.ClientScript.RegisterStartupScript(typeof(string), "Nova1", "window.opener.document.getElementById('hidden').value='" + txt_StorageNo.Text.Trim() + "';window.opener.document.getElementById('form1').submit();top.close();", true);

    }
    protected void ibtnUpdate_Click(object sender, ImageClickEventArgs e)
    {
        if (txt_Mat_Code.Text == "")
        {
            ShowBox.MessageBox(this.Page, "物料编码不能为空！");
            return;
        }



        if (txtNum.Text.ToString() == "")
        {
            ShowBox.MessageBox(this.Page, "数量不能为空！");
            return;
        }
        strSQL = "Update TDelStorage_List SET Mat_Code='" + txt_Mat_Code.Text + "',Num='" + txtNum.Text + "' WHERE PID = '" + strPID + "'";
        cmd = access.GetSqlStringCommond(strSQL);
        int n = access.ExecuteNonQuery(cmd);
        if (n > 0)
        {
            ShowBox.MessageBox(this.Page, "修改成功！");
        }
        else
        {
            ShowBox.MessageBox(this.Page, "修改失败！");
        }
        ibtnClose_Click(sender, e);

    }
    protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
    {
        this.Page.ClientScript.RegisterStartupScript(typeof(string), "Nova1", "window.returnValue='" + txt_StorageNo.Text.ToString().Trim() + "';top.close();", true);

    }
}
